﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace SdmxMl.Common
{
    /// <summary>
    /// Static Class holding informations about supported languages by the editor
    /// </summary>
    public class LangSupport
    {
        #region Constants

        public const string ENGLISH_ID = "en";
        public const string GERMAN_ID = "de";
        public const string FRENCH_ID = "fr";
        public const string ITALIAN_ID = "it";

        #endregion

        #region private C'tor

        private LangSupport()
        {
        }

        static LangSupport()
        {
            configTag = "SupportedLanguages";
            sessionWebTag = "META_LANG_INDEX";
            arrLang = GetSupportedLanguages();
        }

        #endregion

        #region Internal methods

        /// <summary>Array of supported languages</summary>
        private static string[] arrLang;
        private static string configTag;
        private static string sessionWebTag;

        /// <summary>Uses configuration file to know supported language set.</summary>
        /// <returns>array of supported languages abbreviations (like in xml:lang)</returns>
        private static string[] GetSupportedLanguages()
        {
            string err = "Fatal Error: List of supported languages is missing or corrupted in configuration file.";
            string supported = string.Empty;
            if (ConfigurationManager.AppSettings[configTag] != null)
            {
                supported = ConfigurationManager.AppSettings[configTag].Trim().ToLower();
                if (supported == null || supported.Length == 0)
                    throw new ApplicationException(err);
            }
            else
                // Default it
                supported = "en;de;fr;it";
            
            return supported.Split(';');
        }

        #endregion

        #region static Properties

        public static IEnumerable<string> SupportedLanguages
        {
            get
            {
                foreach (string lg in arrLang)
                {
                    yield return lg;
                }
            }
        }


        public static int Count
        {
            get { return GetSupportedLanguages().Length; }
        }

        // Current MetaData language to use
        private static int curLangIndex;
        public static int CurLangIndex
        {
            get 
            {
                // For web application get it from user session (if any)
                if (System.Web.HttpContext.Current != null &&
                    System.Web.HttpContext.Current.Session != null &&
                    System.Web.HttpContext.Current.Session[sessionWebTag] != null)
                    return Convert.ToInt32(System.Web.HttpContext.Current.Session[sessionWebTag]);
                
                return curLangIndex; 
            
            }
            set
            {
                curLangIndex = value;
                if (curLangIndex >= arrLang.Length)
                    curLangIndex = 0;
            }
        }

        /// <summary>Abbreviation defining current language</summary>
        public static string CurLang
        {
            get { return arrLang[CurLangIndex]; }
        }

        #endregion

    }
}
